package com.ios.palette.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ios.palette.dao.IBaseDAO;

@Repository("baseDAO")
@Transactional
public class BaseDAOImpl<T> extends  HibernateDaoSupport implements IBaseDAO<T> {

	@Resource
    public void setSessionFacotry(SessionFactory sessionFacotry) {  
        super.setSessionFactory(sessionFacotry);  
    }
	public T findById(Class<T> entityClass,Serializable id) {
		// TODO Auto-generated method stub
//		System.out.println(entityClass.getSimpleName());
		return (T) this.getHibernateTemplate().get(entityClass, id);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll(Class<T> entityClass) {
		// TODO Auto-generated method stub
		return (List<T>) this.getHibernateTemplate().find("from " + entityClass.getSimpleName());
	}

	public Serializable save(T t) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(t);
	}

	public void update(T t) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(t);
	}

	public void delete(T t) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findOrderByDesc(Class<T> entityClass, String order) {
		// TODO Auto-generated method stub
		List<T> list=(List<T>) this.getHibernateTemplate().find("from "+entityClass.getSimpleName()+" t order by t.? DESC",order);
		return list;
	}
	@Override
	public void delete(Class<T> entityClass, Long id) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().find("delete from "+entityClass.getSimpleName()+" t where t.id=?",id);
	}

}
